<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/include/TagLib.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030">
<title>QQ邮箱 - 写信</title>
<link rel="stylesheet" type="text/css" href="<c:url value='/index_files/comm20100f0f4f.css' />">
<link rel="stylesheet" type="text/css" href="<c:url value='/index_files/getcss.css ' />">
<link href="<c:url value='/index_files/kindedior/themes/default/default.css' />" rel="stylesheet">
<script type="text/javascript" src="<c:url value='/uploadify/jquery-1.6.2.min.js ' />"></script>
<script language="javascript" src="<c:url value='/js/Valid.js' />"></script>
<script src="<c:url value='/index_files/kindedior/kindeditor-min.js' />"></script>
<script src="<c:url value='/index_files/kindedior/lang/zh_CN.js' />"></script>
<script type="text/javascript" src="<c:url value='/uploadify/swfobject.js' />"></script> 
<script type="text/javascript" src="<c:url value='/uploadify/jquery.uploadify.v2.1.4.min.js' />"></script>
<script type="text/javascript">
var editor='',temid='',tempidMailAnnex='';
//初始邮件附件id
var idMailAnnexArry=new Array();
<c:forEach items="${mailAnnexList}" var="file">
	idMailAnnexArry.push('${file.idMailAnnex}');
</c:forEach>
tempidMailAnnex=idMailAnnexArry.join(',');
//加载html控件
$(function($){
	KindEditor.ready(function(K) {
		editor = K.create('textarea[id="content"]', {
				uploadJson:'<c:url value="/index_files/kindedior/jsp/upload_json.jsp" />',
				fileManagerJson : '<c:url value="/index_files/kindedior/jsp/file_manager_json.jsp" />',
				allowFileManager : true
			});
	});
	//收件人
	$('#recPeople').attr("dataType","Require").attr("msg","收件人不能为空！");
});
//发送
function send(){
    if(Validator.Validate(document.frm,2)){
	    $('#btn_true_up,#btn_true_down').val('正在发送...').attr("disabled","disabled").css("width","auto");
	    $('#btn_draft_up,#btn_draft_down').attr("disabled","disabled");
	    //同步数据
		editor.sync();
	    var queryparam ={
			"toAddress":$('#recPeople').val(),
			"toCc":$('#copySend').val(),
			"toBcc":$('#secretSend').val(),
			"subject":$('#subject').val(),
			"content":$('#content').val(),
			"idMailDetail":"${tblMailDetail.idMailDetail}",
			"idUserMail":$('#email').val(),
		    "idMailAnnex":tempidMailAnnex
		 };
		 $.ajax({
			 type: "POST", 
			 url:"<c:url value='/sendEmail.do' />",
			 async:true,
			 data:queryparam,
			 success:
			 function(data) {
			    if(data=="false"){
			    	alert("发送失败，请重新发送！");
			    }else{
			    	 location.href="<c:url value='/sendSuccess.do' />";
			    }
			    $('#btn_true_up,#btn_true_down').val('发送').css("width","auto");
			    $('#btn_true_up')[0].disabled=false;
			    $('#btn_true_down')[0].disabled=false;
    			$('#btn_draft_up')[0].disabled=false;
    			$('#btn_draft_down')[0].disabled=false
			 }
		 });
	};
}
//保存草稿箱
function saveDraft(){
    //同步数据
	editor.sync();
	if(Validator.Validate(document.frm,2)){
	   var queryparam ={
			'toAddress':$('#recPeople').val(),
			'toCc':$('#copySend').val(),
			'toBcc':$('#secretSend').val(),
			'subject':$('#subject').val(),
			'content':$('#content').val(),
			'idMailDetail':"${tblMailDetail.idMailDetail}",
			'idUserMail':$('#email').val(),
			'idMailAnnex':tempidMailAnnex
		};
		$.ajax({
			 type: "POST", 
			 url:"<c:url value='/saveDraft.do' />",
			 async:true,
			 data:queryparam,
			 success:
			 function(data) {
			    if(data=="false"){
			    	alert("保存草稿箱失败！");
			    }else{
			    	 alert("保存草稿箱成功！");
			    	 location.href="<c:url value='/writeEmail.do' />";
			    }
			     
			 }
		 });
	}
}
//显示抄送
function showACC(){
	if($('#trCC').is(":hidden")){
		$('#trCC').show();
		$('#aCC').html("删除抄送");
	}else{
		$('#trCC').hide();
		$('#copySend').val('');
		$('#aCC').html("添加抄送");
	}
}
//显示密送
function showABCC(){
	if($('#trBCC').is(":hidden")){
		$('#trBCC').show();
		$('#aBCC').html("删除密送");
	}else{
		$('#trBCC').hide();
		$('#secretSend').val('');
		$('#aBCC').html("添加密送");
	}
}
//选中联系人
function focuUser(idCompany,idContactPerson,email,username){
	if(temid!=null && temid!=''){
		var msg="\""+username+"\"<"+email+">";
		if($('#'+temid).val()!=''){
			var val=$('#'+temid).val();
			if(val.indexOf(msg)==-1){
				$('#'+temid).val($('#'+temid).val()+';'+msg);
			}
		}else{
		    var val=$('#'+temid).val();
		    if(val.indexOf(msg)==-1){
				$('#'+temid).val(msg);
			}
		}
	}
}

//查询联系人
function queryPerson(obj){
	var val=$(obj).val();
	if(val!=""){
		var queryparam ={
		   		'idCompanyType':val
			};
		$.ajax({
			 type: "POST", 
			 url:"<c:url value='/queryWritePerson.do' />",
			 async:true,
			 data:queryparam,
			 success:
			 function(data) {
			    if(data=="false"){
			    	alert("操作失败！");
			    }else{
			       $('#per').html(data);
			    }
			 }
		 });
	 }else{
	 	$('#per').html("");
	 }
}
//获取临时id
function cacheFocus(obj){
	temid=$(obj).attr("id");
}
//删除文件
function delFile(id){
   if(confirm("确定删除文件吗？")){
		var queryparam ={
		   		'idMailAnnex':id,
		   		'idMailDetail':"${tblMailDetail.idMailDetail}"
			};
		$.ajax({
			 type: "POST", 
			 url:"<c:url value='/deleteAttachmentJQ.do' />",
			 async:true,
			 data:queryparam,
			 success:
			 function(data) {
			    if(data=="false"){
			    	alert("操作失败！");
			    }else{ 
			    	var tempAttr=tempidMailAnnex.split(',');
			    	var temid=new Array();
			    	for(var i=0;i<tempAttr.length;i++){
			    		if(id!=tempAttr[i]){
			    			temid.push(tempAttr[i]);
			    		}
			    	}
			    	//重新赋值
			    	tempidMailAnnex=temid.join(",");
			    	$("#file_"+id).remove();
			    }
			 }
		 });
	}
}
//上传附件
$(document).ready(function() {
	$("#uploadify").uploadify({
	    'uploader'       : '<c:url value="/uploadify/uploadify.swf" />',
	    'script'         : '<c:url value="/uploadFile.do" />;jsessionid=<%=session.getId()%>',
	    'fileDataName'   : 'fileDate',
	    'folder'         : '/',
	    'queueID'        : 'fileQueue',
	    'queueSizeLimit' : 6,
	    'multi'          : false,
	    'auto'           : true,
	    'sizeLimit'      : 1024*1024*1000, 
	    'simUploadLimit' : 2,
	    'buttonImg'      : '<c:url value="/uploadify/attach.png" />', 
	    'height'         : 20,
	    'width'          : 50,
	    'cancelImg'      : '<c:url value="/uploadify/cancel.png" />',
	    'wmode'          : 'transparent',
	    'onComplete'     : function(event, queueId, fileObj, response, data){
	            if(response=='0'){
	            	alert('上传文件失败！');
	            }else{
	    	 		var rtnArr=response.split(";");
	    	 		var id=rtnArr[0];
	    	 		var fileName=rtnArr[1];
	    	 		$("<div id=\"file_"+id+"\">"+fileName+"&nbsp;&nbsp;<a href=\"JavaScript:delFile(\'"+id+"\');\">删除</a></div>").appendTo("#rtn_file");
	    	 		//判断附件是否存在，存在不赋值、不存在赋值
	    	 		if(tempidMailAnnex!=''){
		    	 	 	tempidMailAnnex+=",";
		    	 	 	tempidMailAnnex+=id;
				    	//重新赋值
			    	}else{
			    		tempidMailAnnex=id;
			    	}
	    	 	}
	    },
	    'onError'        : function(event,queueId,fileObj,errorObj){
	    	if(fileObj.size>1024*1024*1000){
	    		alert("你上传的 \“"+fileObj.name+"\” 文件过大，请上传小于1000M的附件。");
	    	}
	    }
	   });
	});
	//显示或隐藏
	function showOrHide(i){
		if($('#content_'+i).is(":hidden")){
			$('#content_'+i).show();
		}else{
			$('#content_'+i).hide();
		}
	}
</script>
	</head>
	<body class="tbody">
		<form method="post" id="frm" name="frm" target="actionFrame" action="" >
			<table cellspacing="0" cellpadding="0" border="0" class="composetab">
				<tbody>
					<tr>
						<td>
							<div class="composetab_img"></div>
							<div class="composetab_sel">
								<div>
									普通邮件
								</div>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<div id="toolbar">
				<div class="toolbg toolbgline" style="background-image: none;">
					<input name="sendbtn" type="button" class="wd1 btn btn_true" value="发送" tabindex="9" onClick="send();" id="btn_true_up">
					<input name="savebtn" type="button" class="wd1 btn" value="存草稿" title="存草稿后，您可以在草稿箱中找回" tabindex="10" id="btn_draft_up" onClick="saveDraft();">
				</div>
			</div>
			<div class="readmailinfo"
				style="border-bottom: 1px solid #fff; min-width: 650px; _zoom: 1;"
				id="sendtimepadding">
				<div id="rightArea"
					style="float: right; width: 179px !important; width: 180px; margin: 16px 12px 0 0; margin: 14px 14px 0 0\9; filter: none;">
					<div id="addr_cmd" class="cptab cpslt" style="_width: 88px;"
						onclick="changeTab('AddrTab');">
						<a>公司类型</a>
					</div>
					<div style="height: 25px; filter: none;" class="grptitle_tab_"></div>
					<div id="AddrTab" class="addrtab cpright" style="">
						<div id="quickaddr_div">
							<div class="lm_sbar">
								<select name="companyType" id="companyType" style="width:100%" onChange="queryPerson(this)">
									<option value="">请选择</option>
									<c:forEach items="${tblCompanyTypesList}" var="type" varStatus="order">
										<option value="${type.IDCOMPANYTYPE}">${type.COMPANYTYPENAME}</option>	
									</c:forEach>
								</select>
							</div>
							<div id="lm13442611600600057955784257501364tree" class="lm_panel">
								<div class="groupopen" style="display: ">
									<div class="settingtable qqshowbd" style="padding: 4px 0 2px 8px;">
										邮箱联系人
									</div>
								</div>
								<div class="groupsub" style="" id="per">
									
								</div>
							</div>
						</div>
					</div>
				</div>
				<div style="float: right; margin: 200px 3px 0 6px !important; margin: 200px 5px 0 3px; height: 220px; x_height: 120px;">
					<a id="rightAreaBtn"
						style="height: 200px; font: bold 12px 宋体; text-decoration: none; color: #585858">
						<input type="button" class="prefd"> </a>
				</div>
				<div style="margin-right: 22px; margin-top: 14px; clear: left;">
					<!--start left -->
					<div style="height: 2px; display: block; * display: none; width: auto;">
						&nbsp;
					</div>
					<table cellpadding="0" cellspacing="0" border="0" class="i" style="width: auto; margin-bottom: 5px;" id="trTO">
						<tbody>
							<tr>
								<td style="width: 58px; * padding-right: 10px;" valign="top" >
									<div
										style="width: 58px; padding-right: 10px; text-align: right; padding-top: 6px" >
										 收件人
									</div>
								</td>
								<td class="content_title" width="99%" style="padding-right: 4px;">
									<div class="div_txt" style="cursor: text;" unselectable="on">
										<div style="margin-right: 32px; height: 16px;">
											<div style="_position: absolute; _width: 100%;">
												<input id="recPeople" title="" name="recPeople" type="text" style="word-break: break-all; height: 16px; line-height: 16px; width: 99%; border-width: 0;" value='${tblMailDetail.recPeople}'  onClick="cacheFocus(this)">
											</div>
										</div>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
					<table cellpadding="0" id="trCC" cellspacing="0" border="0" class="i" style="width:auto;margin-bottom:5px;display:${not empty tblMailDetail.copySend?'block':'none'}" id="trTO">
						<tbody>
							<tr>
								<td style="width: 58px; * padding-right: 10px;" valign="top">
									<div style="width: 58px; padding-right: 10px; text-align: right; padding-top: 6px">
										抄送
									</div>
								</td>
								<td class="content_title" width="99%" style="padding-right: 4px;">
									<div class="div_txt" style="cursor: text;" unselectable="on">
										<div style="margin-right: 32px; height: 16px;">
											<div style="_position: absolute; _width: 100%;">
												<input id="copySend" title="" name="copySend" type="text" style="word-break: break-all; height: 16px; line-height: 16px; width: 99%; border-width: 0;" value='${tblMailDetail.copySend}' onClick="cacheFocus(this)">
											</div>
										</div>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
					<table cellpadding="0" cellspacing="0" border="0" class="i" id="trBCC" style="width: auto; display:${not empty tblMailDetail.secretSend?'block':'none'}; margin-bottom: 5px;">
						<tbody>
							<tr>
								<td style="width: 58px; * padding-right: 10px;" valign="top">
									<div style="width: 58px; padding-right: 10px; text-align: right; padding-top: 6px">
										 密送
									</div>
								</td>
								<td class="content_title" width="99%" style="padding-right: 4px;">
									<div id="bccAreaCtrl" class="noime div_txt" style="cursor: text;">
										<div style="margin-right: 32px; height: 16px;">
											<div style="_position: absolute; _width: 100%;">
												<input id="secretSend" title="" name="secretSend" type="text" style="word-break: break-all; height: 16px; line-height: 16px; width: 99%; border-width: 0;" value='${tblMailDetail.secretSend}' onClick="cacheFocus(this)">
											</div>
										</div>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
					<div
						style="overflow: hidden; min-height: 22px; _zoom: 1; padding-left: 70px;">
						<div class="input_title addrtitle">
							<div style="float: left; white-space: nowrap; padding-left: 1px;">
								<span>
									<a id="aCC" href="javascript:showACC();" show="false">${not empty tblMailDetail.copySend?'删除抄送':'添加抄送'}</a>&nbsp;-&nbsp;
									<a id="aBCC"  href="javascript:showABCC();" show="false">${not empty tblMailDetail.secretSend?'删除密送':'添加密送'}</a>
								</span>
							</div>
							<div class="clr"></div>
						</div>
					</div>
					<table cellpadding="0" cellspacing="0" border="0" class="i" style="width: auto; margin: 5px 0;">
						<tbody>
							<tr>
								<td style="width: 58px; * padding-right: 10px;" valign="top"></td>
								<td width="99%" id="receiverMsgContainer">
									<div style="display: none;"></div>
								</td>
							</tr>
							<tr>
								<td style="width: 58px; * padding-right: 10px;" valign="top" nowrap="">
									<div style="width: 58px; padding-right: 10px; text-align: right; padding-top: 6px" nowrap="">
										<span title="主题是一封邮件的标题。">主题</span>
									</div>
								</td>
								<td class="content_title" width="99%" style="padding-right: 4px;">
									<div class="div_txt" style="cursor: text;" unselectable="on">
										<div style="margin-right: 32px; height: 16px;">
											<div style="_position: absolute; _width: 100%;">
												<input id="subject" title="主题是一封邮件的标题，可不填。" name="subject" type="text" style="word-break: break-all; height: 16px; line-height: 16px; width: 99%; border-width: 0;" value="${tblMailDetail.title}" >
											</div>
										</div>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="input_title"
						style="padding-top: 10px; padding-bottom: 8px;" >
						<div>
							<span id="composecontainer">
								<span class="dragAndDropTrap_box" style="position: relative; margin: 0 8px 0 0; padding-left: 54px;">
									<span title="添加小于1000M的文件作为附件"  id="AttachFrame" style="position: relative;">						  
					    				<input type="file" name="fileDate" id="uploadify" />
									</span>
									<span class="moreupload" id="moreupload">
										<a class="ico_moreupload" onmousedown="return false;"></a>
									</span>
								</span>
							</span>
						</div>
				</div>
				<div style="margin: 0 211px 0 69px; _margin-right: 231px;" id="AttList" class="attbg">
					<div id="rtn_file" style="padding: 7px;" id="editor_bgmusic_container" >
						<c:forEach items="${mailAnnexList}" var="file">
							<div id="file_${file.idMailAnnex}">${fn:substringAfter(file.filename,'/')}&nbsp;&nbsp;<a href="JavaScript:delFile('${file.idMailAnnex}');">删除</a></div>
						</c:forEach>
					</div>
				</div>
				<div style="margin-right: 20px; margin-top: -5px;">
					<table cellpadding="0" class="i" cellspacing="0" border="0" style="width: auto;">
						<tbody>
							<tr>
								<td style="width: 58px; * padding-right: 10px;" valign="top">
									<div
										style="width: 58px; padding-right: 10px; text-align: right; padding-top: 6px">
										正文
									</div>
								</td>
								<td class="content_title" style="padding-top: 1px !important; padding-top: 0; padding-right: 0; width: 99%;" valign="top">
									<textarea id="content" name="content" style="width: 100%; height: 385;">${tblMailDetail.content}</textarea>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<div style="margin-top: 5px; height: auto; float: left; width: 100%;" class="settingtable">
					<div style="margin: 8px 0 0 68px">
						<div style="float: left; height: 20px;">
							<div id="Senderdiv" style="float: left;">
									发件人：
									<select name="email" id="email">
										<c:forEach items="${userMailList}" var="mail">
											<option value="${mail.idUserMail}" ${defEmail==mail.idUserMail?'selected':''}>${mail.email}</option>
										</c:forEach>
									</select>
							</div>
						</div>
						<div class="clr"></div>
					</div>
				</div>
				<div class="clr"></div>
			</div>
			<div id="toolbar" class="clear">
				<div class="toolbg toolbgline">
					<input name="sendbtn" type="button" class="wd1 btn btn_true" value="发送" tabindex="9" onClick="send();" id="btn_true_down">
					<input name="savebtn" type="button" class="wd1 btn" value="存草稿" title="存草稿后，您可以在草稿箱中找回" tabindex="10" id="btn_draft_down" onClick="saveDraft();">
				</div>
			</div>
		</form>
	</body>
</html>